package com.lc.afbx.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lc.afbx.BaseApplication;
import com.lc.afbx.R;
import com.lc.afbx.adapters.BaoDanAdapter;
import com.lc.afbx.conn.GetApiMyOrderRecommend;
import com.lc.afbx.util.DateTimePickDialogUtil;
import com.lc.afbx.util.PrefrenceConfig;
import com.lc.afbx.util.TimeUtil;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by Administrator on 2016/5/23.
 */
public class PolicyIRecommendActivity extends BaseActivity implements View.OnClickListener {


    private TextView policy_recommend_endtime_tv;
    private TextView policy_recommend_starttime_tv;
    private RelativeLayout policy_recommend_starttime_rl;
    private RelativeLayout policy_recommend_endtime_rl;
    private String initDateTime;
    private String initEndTime;
    private Button policy_recommend_bt;

    //两个textView  车险  寿险
    private TextView policy_recommend_tab_car_tv;
    private TextView policy_recommend_tab_life_tv;

    private String start;
    private String end;
    private TextView policy_recommend_tab_car_line_tv;
    private TextView policy_recommend_tab_life_line_tv;

    private ListView policy_recommend_lv;
    private LinearLayout titleItem;

    private BaoDanAdapter adapter;
    private List<GetApiMyOrderRecommend.Data> lists = new ArrayList();
    private GetApiMyOrderRecommend getApiMyOrderRecommend = new GetApiMyOrderRecommend(new AsyCallBack<GetApiMyOrderRecommend.Info>() {
        @Override
        public void onSuccess(String toast, int type, GetApiMyOrderRecommend.Info info) throws Exception {

            lists.clear();

            lists.addAll(info.dataList);

            policy_recommend_lv.setAdapter(adapter = new BaoDanAdapter(context, lists, false));


        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(context, toast);

        }
    });




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_policy_recommend);

        Calendar calendar = Calendar.getInstance();
        //dialog显示当前时间
        initDateTime = calendar.get(Calendar.YEAR) + "年" + (calendar.get(Calendar.MONTH) - 3) + "月" + calendar.get(Calendar.DAY_OF_MONTH) + "日";
        initEndTime = calendar.get(Calendar.YEAR) + "年" + calendar.get(Calendar.MONTH) + "月" + calendar.get(Calendar.DAY_OF_MONTH) + "日";
        //txt显示默认时间
//        hint = calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) - 2) + "-" + calendar.get(Calendar.DAY_OF_MONTH);
//        endHint = calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + calendar.get(Calendar.DAY_OF_MONTH);

        policy_recommend_endtime_tv = ((TextView) findViewById(R.id.policy_recommend_endtime_tv));
        policy_recommend_starttime_tv = ((TextView) findViewById(R.id.policy_recommend_starttime_tv));

        //布局  添加点击监听
        policy_recommend_starttime_rl = ((RelativeLayout) findViewById(R.id.policy_recommend_starttime_rl));
        policy_recommend_endtime_rl = ((RelativeLayout) findViewById(R.id.policy_recommend_endtime_rl));

        policy_recommend_bt = ((Button) findViewById(R.id.policy_recommend_bt));

        policy_recommend_lv = ((ListView) findViewById(R.id.policy_recommend_lv));

        titleItem = ((LinearLayout) findViewById(R.id.tab_myBaoDan));

        //tab
        policy_recommend_tab_car_tv = ((TextView) findViewById(R.id.policy_recommend_tab_car_tv));
        policy_recommend_tab_life_tv = ((TextView) findViewById(R.id.policy_recommend_tab_life_tv));
        //tabline
        policy_recommend_tab_life_line_tv = ((TextView) findViewById(R.id.policy_recommend_tab_life_line_tv));
        policy_recommend_tab_car_line_tv = ((TextView) findViewById(R.id.policy_recommend_tab_car_line_tv));



        setListener();
    }

    private void setListener() {

        policy_recommend_starttime_rl.setOnClickListener(this);
        policy_recommend_endtime_rl.setOnClickListener(this);
        policy_recommend_bt.setOnClickListener(this);
        policy_recommend_tab_car_tv.setOnClickListener(this);
        policy_recommend_tab_life_tv.setOnClickListener(this);


        //TODO   下面是详情  需要就写 不需要就删了

//        myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//
//                Intent intent = null;
//                if (dataList.get(position) instanceof GetApiMyOrder.Business) {
//
//                    intent = new Intent(context, PolicyDetailsCarActivity.class);
//                    GetApiMyOrder.Business business = (GetApiMyOrder.Business) dataList.get(position);
//                    intent.putExtra("id", business.id);
//
//                } else if (dataList.get(position) instanceof GetApiMyOrder.Accident) {
//
//                    intent = new Intent(context, PolicyDetailsCasualtyActivity.class);
//                    GetApiMyOrder.Accident accident = (GetApiMyOrder.Accident) dataList.get(position);
//                    intent.putExtra("id", accident.id);
//
////                    intent = new Intent(context,);
//
//                } else {
//                    UtilToast.show(context, "dataList没数据咋地");
//                }
//
//                startActivity(intent);
//
//            }
//        });


    }




    @Override
    public void onClick(View view) {

        DateTimePickDialogUtil dialogUtil = null;

        switch (view.getId()) {

            case R.id.back:
                finish();
                return;

            case R.id.policy_recommend_starttime_rl:

                dialogUtil = new DateTimePickDialogUtil(this, initDateTime);
                dialogUtil.dateTimePicKDialog(policy_recommend_starttime_tv);

                return;

            case R.id.policy_recommend_endtime_rl:
                dialogUtil = new DateTimePickDialogUtil(this, initEndTime);
                dialogUtil.dateTimePicKDialog(policy_recommend_endtime_tv);
                return;

            case R.id.policy_recommend_bt:

                if (policy_recommend_starttime_tv.getText().toString().isEmpty() || policy_recommend_endtime_tv.getText().toString().isEmpty()) {
                    UtilToast.show(context, "请选择时间");
                    return;
                }
                start = TimeUtil.dataOne(policy_recommend_starttime_tv.getText().toString());
                end = TimeUtil.dataOne(policy_recommend_endtime_tv.getText().toString());

                if (Long.valueOf(end) - Long.valueOf(start) >= 0) {

                    policy_recommend_tab_car_tv.setTextColor(getResources().getColor(R.color.baodanTab));
                    titleItem.setVisibility(View.VISIBLE);
                    //这是下面的线
                    policy_recommend_tab_life_line_tv.setVisibility(View.GONE);
                    policy_recommend_tab_car_line_tv.setVisibility(View.VISIBLE);

                    getApiMyOrderRecommend.uid = BaseApplication.BasePreferences.readUserId();
                    getApiMyOrderRecommend.type = PrefrenceConfig.ONE;
                    getApiMyOrderRecommend.starttime = start;
                    getApiMyOrderRecommend.endtime = end;

                    getApiMyOrderRecommend.execute(this);

                    return;
                } else {
                    UtilToast.show(context, "起始时间 大于 结束时间");
                    return;
                }

            case R.id.policy_recommend_tab_car_tv:

                policy_recommend_tab_life_line_tv.setVisibility(View.GONE);
                policy_recommend_tab_car_line_tv.setVisibility(View.VISIBLE);

                policy_recommend_tab_car_tv.setTextColor(getResources().getColor(R.color.baodanTab));
                policy_recommend_tab_life_tv.setTextColor(getResources().getColor(R.color.textcolorHei));

                getApiMyOrderRecommend.uid = BaseApplication.BasePreferences.readUserId();
                getApiMyOrderRecommend.type = PrefrenceConfig.ONE;
                getApiMyOrderRecommend.starttime = start;
                getApiMyOrderRecommend.endtime = end;

                getApiMyOrderRecommend.execute(this);

                return;

            case R.id.policy_recommend_tab_life_tv:

                policy_recommend_tab_car_line_tv.setVisibility(View.GONE);
                policy_recommend_tab_life_line_tv.setVisibility(View.VISIBLE);


                policy_recommend_tab_life_tv.setTextColor(getResources().getColor(R.color.baodanTab));
                policy_recommend_tab_car_tv.setTextColor(getResources().getColor(R.color.textcolorHei));

                getApiMyOrderRecommend.uid = BaseApplication.BasePreferences.readUserId();
                getApiMyOrderRecommend.type = PrefrenceConfig.TWO;
                getApiMyOrderRecommend.starttime = start;
                getApiMyOrderRecommend.endtime = end;

                getApiMyOrderRecommend.execute(this);

                return;

        }


    }


}
